package com.commonmail.controller;



import com.commonmail.utils.MailUtils;

import com.commonmail.utils.RandomUtils;
import com.commonmail.utils.RedisUtils;
import com.commonmail.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "邮箱登陆")
@RestController
@RequestMapping("test")
public class MailController {
    @Resource
    private MailUtils mailUtils;
    @Resource
    private RedisUtils redisUtils;
  /*  @Resource
    private TestService testService;*/
    @Resource
    private TokenUtils tokenUtils;

    /* 发送验证码 */
    @ApiOperation("发送验证码")
    @GetMapping("/sendmail")
    public Object sendMail(@ApiParam(value = "请求头", required = true) HttpServletRequest request) {
        Map<String, Object> dto = new HashMap<>();
        String to = request.getHeader("to");
        //判断是否已经发送过验证码
        String code = (String) redisUtils.get(to);
        if (code != null) {
            dto.put("msg", "验证码10分钟内有效");
            dto.put("code", 6000);
            return dto;
        }

        //生产随机数
        Integer ran = RandomUtils.getNumByLen(6);
        String title = "登陆";
        String content = "验证码" + ran + "，5分钟内有效";
        //发送验证码
        if (mailUtils.sendMail(to, title, content)) {
            // 把验证码保存到redis中
            redisUtils.setWithTime(to, ran.toString(), 300);
            dto.put("msg", "发送成功");
            dto.put("code", 2);
            return dto;
        }
        dto.put("msg", "发送失败");
        dto.put("code", -2);
        return dto;
    }

    /* 邮箱登陆 */
    @ApiOperation("邮箱登陆")
    @GetMapping("/maillogin")
    public Object mailLogin(HttpServletRequest request) {
        String to = request.getHeader("to");
        String code = request.getHeader("code");
        Map<String, Object> dto = new HashMap<>();
        //从redis中获取验证吗
        String rCode = (String) redisUtils.get(to);

        if (!rCode.equals(code)) {
            dto.put("msg", "验证码错误");
            dto.put("code", 0);
            return dto;
        }

        //判断用户是否存在
     /*   Test test = testService.getUserByEmail(to);
        if (test == null) {//新用户
            test = new Test();
            test.setCode(to);
            //添加用户到数据库
            *//* ---------------------      添加并获取id      ------------- *//*
            if (!testService.addUser(test)) {
                dto.put("msg", "登陆失败");
                dto.put("code", -1);
                return dto;
            }
        }

        //生成token
        String token = tokenUtils.getToken("Mail-", test.getId(), RandomUtils.genNumByLen(6));
        //把token存入redis中 30分钟有效
        redisUtils.setWithTime(token, JSONObject.toJSONString(test), 30 * 60, TimeUnit.SECONDS);
        dto.put("msg", "登陆成功");
        dto.put("code", 1);
        dto.put("token", token);*/
        return dto;
    }

    /**
     * 用于测试token的替换
     *
     * @param request 请求对象
     * @return
     */
    @ApiOperation("用于测试token的替换")
    @GetMapping("getUser")
    public Object getUser(@ApiParam(value = "请求对象") HttpServletRequest request) {
        Map<String, Object> dto = new HashMap<>();
        String token = request.getHeader("token");
     /*   //判断token存在
        if (!redisUtils.hasKey(token)) {
            dto.put("msg", "token失效");
            dto.put("code", -3);
            return dto;
        }*/
        //把redis转换成对象
     /*   Test test = JSONObject.parseObject(redisUtils.get(token), Test.class);
        token = tokenUtils.replaceToken(token, "Mail-", test);*/
        dto.put("msg", "token有效");
        dto.put("code", 3);
        dto.put("token", token);
        return dto;
    }
}


